home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / wgdb-42.lha / wgdb-4.2 / gdb / m88k-tdep.c < prev    next >
C/C++ Source or Header  |  1992-09-11  |  17KB  |  595 lines

  1. /* Copyright (C) 1988, 1990 Free Software Foundation, Inc.
  2.  
  3. This file is part of GDB.
  4.  
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9.  
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include <stdio.h>
  20. #include "defs.h"
  21. #include "param.h"
  22. #include "frame.h"
  23. #include "inferior.h"
  24. #include "value.h"
  25.  
  26. #ifdef USG
  27. #include <sys/types.h>
  28. #endif
  29.  
  30. #include <sys/param.h>
  31. #include <sys/dir.h>
  32. #include <signal.h>
  33. #include "gdbcore.h"
  34. #include <sys/user.h>
  35. #ifndef USER            /* added to support BCS ptrace_user */
  36.  
  37. #define USER ptrace_user
  38. #endif
  39. #include <sys/ioctl.h>
  40. #include <fcntl.h>
  41.  
  42. #include <sys/file.h>
  43. #include <sys/stat.h>
  44.  
  45. #include "symtab.h"
  46. #include "setjmp.h"
  47. #include "value.h"
  48.  
  49. int stack_error;
  50. jmp_buf stack_jmp;
  51.  
  52. void
  53. tdesc_error_function  (environment, continuable, message)
  54. dc_word_t environment;
  55. dc_boolean_t continuable;
  56. char *message;
  57. {
  58.   if (stack_error) longjmp (stack_jmp, 1);
  59.   if (!continuable)
  60.     {
  61.       printf("%s\n",message);
  62.       abort();
  63.     }
  64. }
  65.  
  66.  
  67. void
  68. tdesc_read_function (environment, memory, length, buffer)
  69. dc_word_t environment;
  70. dc_word_t memory;
  71. int length;
  72. char *buffer;
  73. {
  74.   int ptrace_code;
  75.   errno = 0;
  76.   if (memory < 2048) 
  77. #if 0
  78.     /* This is a no-op!  It sets buffer, but doesn't do anything to
  79.        what buffer points to.  What does this function do anyway?
  80.        And this is wrong for cross-debugging.  */
  81.     buffer = ptrace (3, inferior_pid, memory, 0);
  82. #else
  83.     return;
  84. #endif
  85.   else
  86.     read_memory (memory, buffer, length);
  87. }
  88.  
  89. /* Map function for tdesc */
  90. void
  91. tdesc_map_function (map_env, loc, map_info_in, map_info_out)
  92. dc_word_t map_env;
  93. dc_word_t loc;
  94. dc_map_info_in_t map_info_in;
  95. dc_map_info_out_t *map_info_out;
  96. {
  97. int map_flags = DC_MIO_ENTRY_POINT | DC_MIO_IMPLICIT_PROLOGUE_END;
  98. int entry_point = get_pc_function_start(loc);
  99. map_info_out->flags = map_flags;
  100. map_info_out->entry_point = entry_point;
  101. }
  102.  
  103. dc_handle_t tdesc_handle;
  104.  
  105. extern int debug_info;
  106.  
  107. void
  108. init_tdesc ()
  109. {
  110.   tdesc_handle = dc_initiate (debug_info, tdesc_error_function,
  111.                   0,tdesc_read_function,0,0,0,0,0,tdesc_map_function,0);
  112. dc_dcontext_t current_context;
  113.   
  114. /* setup current context, called from wait_for_inferior */
  115.  
  116. dc_dcontext_t
  117. init_dcontext()
  118. {
  119.   dc_word_t reg_info[DC_NUM_REG];
  120.   dc_word_t reg_flags[2] = {0,-1};
  121.   dc_word_t aux_info[DC_NUM_AUX];
  122.   dc_word_t aux_flags[2] = {0,-1};
  123.   dc_exactness_t loc_exact = DC_NO;
  124.   dc_word_t psr_info;
  125.   dc_boolean_t psr_ind = 0;
  126.   dc_word_t psr_flags[2] = {0,-1};
  127.  
  128.   bcopy (®isters, reg_info, DC_NUM_REG * 4);
  129.   aux_info[DC_AUX_LOC] =  read_register(SXIP_REGNUM);
  130.   aux_info[DC_AUX_SXIP] = read_register(SXIP_REGNUM);
  131.   aux_info[DC_AUX_SNIP] = read_register(SNIP_REGNUM);
  132.   aux_info[DC_AUX_SFIP] = read_register(SFIP_REGNUM);
  133.   aux_info[DC_AUX_FPSR] = read_register(FPSR_REGNUM);
  134.   aux_info[DC_AUX_FPCR] = read_register(FPCR_REGNUM);
  135.  
  136.   psr_info = read_register(PSR_REGNUM);
  137.  
  138.   return dc_make_dcontext (tdesc_handle, reg_info, reg_flags, aux_info,
  139.                          aux_flags, loc_exact, psr_info, psr_ind, psr_flags);
  140. }
  141.  
  142.  
  143. dc_dcontext_t
  144. get_prev_context (context)
  145.   dc_dcontext_t context;
  146. {
  147.   return current_context = dc_previous_dcontext (context); 
  148. }
  149.   
  150.  
  151.  
  152.  
  153. /* Determine frame base for this file's frames.  This will be either
  154.    the CFA or the old style FP_REGNUM; the symtab for the current pc's
  155.    file has the information                          */
  156.  
  157. CORE_ADDR
  158. get_frame_base(pc)
  159. CORE_ADDR pc;
  160. {
  161.   struct symtab *this_file = find_pc_symtab(pc);
  162.   int coffsem_frame_position;
  163.   
  164.   /* If this_file is null, there's a good chance the file was compiled
  165.      without -g.  If that's the case, use CFA (canonical frame addr) 
  166.      as the default frame pointer.                                 */
  167.  
  168.   if (this_file)
  169.     {
  170.     coffsem_frame_position = this_file->coffsem & 3;
  171.     if (coffsem_frame_position == 1) 
  172.       return (CORE_ADDR) dc_general_register (current_context, FP_REGNUM);
  173.     else
  174.       /* default is CFA, as well as if coffsem==2 */
  175.       return (CORE_ADDR) dc_frame_address  (current_context);
  176.     }
  177.  
  178.   return (CORE_ADDR) dc_frame_address (current_context);
  179. }
  180.  
  181. #if TARGET_BYTE_ORDER != HOST_BYTE_ORDER
  182. you lose
  183. #else /* Host and target byte order the same.  */
  184. #define SINGLE_EXP_BITS  8
  185. #define DOUBLE_EXP_BITS 11
  186. int
  187. IEEE_isNAN(fp, len)
  188.      int *fp, len;
  189.      /* fp points to a single precision OR double precision
  190.       * floating point value; len is the number of bytes, either 4 or 8.
  191.       * Returns 1 iff fp points to a valid IEEE floating point number.
  192.       * Returns 0 if fp points to a denormalized number or a NaN
  193.       */
  194. {
  195.   int exponent;
  196.   if (len == 4)
  197.     {
  198.       exponent = *fp;
  199.       exponent = exponent << 1 >> (32 - SINGLE_EXP_BITS - 1);
  200.       return ((exponent == -1) || (! exponent && *fp));
  201.     }
  202.   else if (len == 8)
  203.     {
  204.       exponent = *(fp+1);
  205.       exponent = exponent << 1 >> (32 - DOUBLE_EXP_BITS - 1);
  206.       return ((exponent == -1) || (! exponent && *fp * *(fp+1)));
  207.     }
  208.   else return 1;
  209. }
  210. #endif /* Host and target byte order the same.  */
  211.  
  212. #define FIRST_PRESERVED_REGNUM    14
  213. #define LAST_PRESERVED_REGNUM    25
  214. #define FIRST_PARM_REGNUM    2
  215. #define LAST_PARM_REGNUM    9
  216.  
  217. #define MAX_REG_PARMS    (LAST_PARM_REGNUM - FIRST_PARM_REGNUM + 1)
  218.  
  219. void
  220. frame_find_saved_regs (fi, fsr)
  221.       struct frame_info *fi;
  222.       struct frame_saved_regs *fsr;
  223. {
  224.    register int regnum;
  225.  
  226.    error ("Feature not implemented for the 88k yet.");
  227.    return;
  228.  
  229. #if 0
  230.    for (regnum = FIRST_PARM_REGNUM; regnum <= LAST_PARM_REGNUM; regnum++)
  231.      fsr->regs[regnum]
  232.         = (unsigned) fi->frame - ((regnum - FIRST_PARM_REGNUM) * 4);
  233.  
  234.    fsr->regs[SP_REGNUM] = 0;        /* SP not saved in frames */
  235.    fsr->regs[FP_REGNUM] = fi->frame;
  236.    fsr->regs[PC_REGNUM] = fi->frame + 4;
  237. #endif
  238. }
  239.  
  240. static int
  241. pushed_size (prev_words, v)
  242.      int prev_words;
  243.      struct value *v;
  244. {
  245.   switch (TYPE_CODE (VALUE_TYPE (v)))
  246.     {
  247.       case TYPE_CODE_VOID:        /* Void type (values zero length) */
  248.  
  249.     return 0;    /* That was easy! */
  250.  
  251.       case TYPE_CODE_PTR:        /* Pointer type */
  252.       case TYPE_CODE_ENUM:        /* Enumeration type */
  253.       case TYPE_CODE_INT:        /* Integer type */
  254.       case TYPE_CODE_REF:        /* C++ Reference types */
  255.       case TYPE_CODE_ARRAY:        /* Array type, lower bound zero */
  256.  
  257.     return 1;
  258.  
  259.       case TYPE_CODE_FLT:        /* Floating type */
  260.  
  261.     if (TYPE_LENGTH (VALUE_TYPE (v)) == 4)
  262.       return 1;
  263.     else
  264.       /* Assume that it must be a double.  */
  265.       if (prev_words & 1)        /* at an odd-word boundary */
  266.         return 3;            /* round to 8-byte boundary */
  267.       else
  268.         return 2;
  269.  
  270.       case TYPE_CODE_STRUCT:        /* C struct or Pascal record */
  271.       case TYPE_CODE_UNION:        /* C union or Pascal variant part */
  272.  
  273.     return (((TYPE_LENGTH (VALUE_TYPE (v)) + 3) / 4) * 4);
  274.  
  275.       case TYPE_CODE_FUNC:        /* Function type */
  276.       case TYPE_CODE_SET:        /* Pascal sets */
  277.       case TYPE_CODE_RANGE:        /* Range (integers within bounds) */
  278.       case TYPE_CODE_PASCAL_ARRAY:    /* Array with explicit type of index */
  279.       case TYPE_CODE_MEMBER:        /* Member type */
  280.       case TYPE_CODE_METHOD:        /* Method type */
  281.     /* Don't know how to pass these yet.  */
  282.  
  283.       case TYPE_CODE_UNDEF:        /* Not used; catches errors */
  284.       default:
  285.     abort ();
  286.     }
  287. }
  288.  
  289. static void
  290. store_parm_word (address, val)
  291.      CORE_ADDR address;
  292.      int val;
  293. {
  294.   write_memory (address, &val, 4);
  295. }
  296.  
  297. static int
  298. store_parm (prev_words, left_parm_addr, v)
  299.      unsigned int prev_words;
  300.      CORE_ADDR left_parm_addr;
  301.      struct value *v;
  302. {
  303.   CORE_ADDR start = left_parm_addr + (prev_words * 4);
  304.   int *val_addr = (int *)VALUE_CONTENTS(v);
  305.  
  306.   switch (TYPE_CODE (VALUE_TYPE (v)))
  307.     {
  308.       case TYPE_CODE_VOID:        /* Void type (values zero length) */
  309.  
  310.     return 0;
  311.  
  312.       case TYPE_CODE_PTR:        /* Pointer type */
  313.       case TYPE_CODE_ENUM:        /* Enumeration type */
  314.       case TYPE_CODE_INT:        /* Integer type */
  315.       case TYPE_CODE_ARRAY:        /* Array type, lower bound zero */
  316.       case TYPE_CODE_REF:        /* C++ Reference types */
  317.  
  318.     store_parm_word (start, *val_addr);
  319.     return 1;
  320.  
  321.       case TYPE_CODE_FLT:        /* Floating type */
  322.  
  323.     if (TYPE_LENGTH (VALUE_TYPE (v)) == 4)
  324.       {
  325.         store_parm_word (start, *val_addr);
  326.         return 1;
  327.       }
  328.     else
  329.       {
  330.         store_parm_word (start + ((prev_words & 1) * 4), val_addr[0]);
  331.         store_parm_word (start + ((prev_words & 1) * 4) + 4, val_addr[1]);
  332.         return 2 + (prev_words & 1);
  333.       }
  334.  
  335.       case TYPE_CODE_STRUCT:        /* C struct or Pascal record */
  336.       case TYPE_CODE_UNION:        /* C union or Pascal variant part */
  337.  
  338.     {
  339.       unsigned int words = (((TYPE_LENGTH (VALUE_TYPE (v)) + 3) / 4) * 4);
  340.       unsigned int word;
  341.  
  342.       for (word = 0; word < words; word++)
  343.         store_parm_word (start + (word * 4), val_addr[word]);
  344.       return words;
  345.     }
  346.  
  347.       default:
  348.     abort ();
  349.     }
  350. }
  351.  
  352.  /* This routine sets up all of the parameter values needed to make a pseudo
  353.     call.  The name "push_parameters" is a misnomer on some archs,
  354.     because (on the m88k) most parameters generally end up being passed in
  355.     registers rather than on the stack.  In this routine however, we do
  356.     end up storing *all* parameter values onto the stack (even if we will
  357.     realize later that some of these stores were unnecessary).  */
  358.  
  359. void
  360. push_parameters (return_type, struct_conv, nargs, args)
  361.       struct type *return_type; 
  362.       int struct_conv;
  363.       int nargs;
  364.       value *args;
  365.  {
  366.    int parm_num;
  367.    unsigned int p_words = 0;
  368.    CORE_ADDR left_parm_addr;
  369.  
  370.    /* Start out by creating a space for the return value (if need be).  We
  371.       only need to do this if the return value is a struct or union.  If we
  372.       do make a space for a struct or union return value, then we must also
  373.       arrange for the base address of that space to go into r12, which is the
  374.       standard place to pass the address of the return value area to the
  375.       callee.  Note that only structs and unions are returned in this fashion.
  376.       Ints, enums, pointers, and floats are returned into r2.  Doubles are
  377.       returned into the register pair {r2,r3}.  Note also that the space
  378.       reserved for a struct or union return value only has to be word aligned
  379.       (not double-word) but it is double-word aligned here anyway (just in
  380.       case that becomes important someday).  */
  381.  
  382.    switch (TYPE_CODE (return_type))
  383.      {
  384.        case TYPE_CODE_STRUCT:
  385.        case TYPE_CODE_UNION:
  386.          {
  387.            int return_bytes = ((TYPE_LENGTH (return_type) + 7) / 8) * 8;
  388.            CORE_ADDR rv_addr;
  389.  
  390.            rv_addr = read_register (SP_REGNUM) - return_bytes;
  391.  
  392.            write_register (SP_REGNUM, rv_addr); /* push space onto the stack */
  393.            write_register (SRA_REGNUM, rv_addr);/* set return value register */
  394.          }
  395.      }
  396.  
  397.    /* Here we make a pre-pass on the whole parameter list to figure out exactly
  398.       how many words worth of stuff we are going to pass.  */
  399.  
  400.    for (p_words = 0, parm_num = 0; parm_num < nargs; parm_num++)
  401.      p_words += pushed_size (p_words, value_arg_coerce (args[parm_num]));
  402.  
  403.    /* Now, check to see if we have to round up the number of parameter words
  404.       to get up to the next 8-bytes boundary.  This may be necessary because
  405.       of the software convention to always keep the stack aligned on an 8-byte
  406.       boundary.  */
  407.  
  408.    if (p_words & 1)
  409.      p_words++;        /* round to 8-byte boundary */
  410.  
  411.    /* Now figure out the absolute address of the leftmost parameter, and update
  412.       the stack pointer to point at that address.  */
  413.  
  414.    left_parm_addr = read_register (SP_REGNUM) - (p_words * 4);
  415.    write_register (SP_REGNUM, left_parm_addr);
  416.  
  417.    /* Now we can go through all of the parameters (in left-to-right order)
  418.       and write them to their parameter stack slots.  Note that we are not
  419.       really "pushing" the parameter values.  The stack space for these values
  420.       was already allocated above.  Now we are just filling it up.  */
  421.  
  422.    for (p_words = 0, parm_num = 0; parm_num < nargs; parm_num++)
  423.      p_words +=
  424.        store_parm (p_words, left_parm_addr, value_arg_coerce (args[parm_num]));
  425.  
  426.    /* Now that we are all done storing the parameter values into the stack, we
  427.       must go back and load up the parameter registers with the values from the
  428.       corresponding stack slots.  Note that in the two cases of (a) gaps in the
  429.       parameter word sequence causes by (otherwise) misaligned doubles, and (b)
  430.       slots correcponding to structs or unions, the work we do here in loading
  431.       some parameter registers may be unnecessary, but who cares?  */
  432.  
  433.    for (p_words = 0; p_words < 8; p_words++)
  434.      {
  435.        write_register (FIRST_PARM_REGNUM + p_words,
  436.          read_memory_integer (left_parm_addr + (p_words * 4), 4));
  437.      }
  438. }
  439.  
  440. void
  441. pop_frame ()
  442. {
  443.   error ("Feature not implemented for the m88k yet.");
  444.   return;
  445. }
  446.  
  447.  void
  448.  collect_returned_value (rval, value_type, struct_return, nargs, args)
  449.       value *rval;
  450.       struct type *value_type;
  451.       int struct_return;
  452.       int nargs;
  453.       value *args;
  454.  {
  455.    char retbuf[REGISTER_BYTES];
  456.  
  457.    bcopy (registers, retbuf, REGISTER_BYTES);
  458.    *rval = value_being_returned (value_type, retbuf, struct_return);
  459.    return;
  460.  }
  461.  
  462. #if 0
  463. /* Now handled in a machine independent way with CALL_DUMMY_LOCATION.  */
  464.  /* Stuff a breakpoint instruction onto the stack (or elsewhere if the stack
  465.     is not a good place for it).  Return the address at which the instruction
  466.     got stuffed, or zero if we were unable to stuff it anywhere.  */
  467.   
  468.  CORE_ADDR
  469.  push_breakpoint ()
  470.   {
  471.    static char breakpoint_insn[] = BREAKPOINT;
  472.    extern CORE_ADDR text_end;    /* of inferior */
  473.    static char readback_buffer[] = BREAKPOINT;
  474.    int i;
  475.   
  476.    /* With a little bit of luck, we can just stash the breakpoint instruction
  477.       in the word just beyond the end of normal text space.  For systems on
  478.       which the hardware will not allow us to execute out of the stack segment,
  479.       we have to hope that we *are* at least allowed to effectively extend the
  480.       text segment by one word.  If the actual end of user's the text segment
  481.       happens to fall right at a page boundary this trick may fail.  Note that
  482.       we check for this by reading after writing, and comparing in order to
  483.       be sure that the write worked.  */
  484.  
  485.    write_memory (text_end, &breakpoint_insn, 4);
  486.  
  487.    /* Fill the readback buffer with some garbage which is certain to be
  488.       unequal to the breakpoint insn.  That way we can tell if the
  489.       following read doesn't actually succeed.  */
  490.  
  491.    for (i = 0; i < sizeof (readback_buffer); i++)
  492.      readback_buffer[i] = ~ readback_buffer[i];    /* Invert the bits */
  493.  
  494.    /* Now check that the breakpoint insn was successfully installed.  */
  495.  
  496.    read_memory (text_end, readback_buffer, sizeof (readback_buffer));
  497.    for (i = 0; i < sizeof (readback_buffer); i++)
  498.      if (readback_buffer[i] != breakpoint_insn[i])
  499.        return 0;        /* Failed to install! */
  500.  
  501.    return text_end;
  502.  }
  503. #endif
  504.  
  505. /* Like dc_psr_register but takes an extra int arg.  */
  506. static dc_word_t
  507. psr_register (context, dummy)
  508.      dc_dcontext_t context;
  509.      int dummy;
  510. {
  511.   return dc_psr_register (context);
  512. }
  513.  
  514. /* Same functionality as get_saved_register in findvar.c, but implemented
  515.    to use tdesc.  */
  516. void
  517. get_saved_register (raw_buffer, optim, addrp, frame, regnum, lvalp)
  518.      char *raw_buffer;
  519.      int *optim;
  520.      CORE_ADDR *addrp;
  521.      FRAME frame;
  522.      int regnum;
  523.      enum lval_type *lvalp;
  524. {
  525.   struct frame_info *fi = get_frame_info (frame);
  526.   
  527.   /* Functions to say whether a register is optimized out, and
  528.      if not, to get the value.  Take as args a context and the
  529.      value of get_reg_arg.  */
  530.   int (*get_reg_state) ();
  531.   dc_word_t (*get_reg) ();
  532.   int get_reg_arg;
  533.  
  534.   /* Because tdesc doesn't tell us whether it got it from a register
  535.      or memory, always say we don't have an address for it.  */
  536.   if (addrp != NULL)
  537.     *addrp = 0;
  538.   
  539.   if (regnum < DC_NUM_REG)
  540.     {
  541.       get_reg_state = dc_general_register_state;
  542.       get_reg = dc_general_register;
  543.       get_reg_arg = regnum;
  544.     }
  545.   else
  546.     {
  547.       get_reg_state = dc_auxiliary_register_state;
  548.       get_reg = dc_auxiliary_register;
  549.       switch (regnum)
  550.     {
  551.     case SXIP_REGNUM:
  552.       get_reg_arg = DC_AUX_SXIP;
  553.       break;
  554.     case SNIP_REGNUM:
  555.       get_reg_arg = DC_AUX_SNIP;
  556.       break;
  557.     case FPSR_REGNUM:
  558.       get_reg_arg = DC_AUX_FPSR;
  559.       break;
  560.     case FPCR_REGNUM:
  561.       get_reg_arg = DC_AUX_FPCR;
  562.       break;
  563.     case PSR_REGNUM:
  564.       get_reg_state = dc_psr_register_bit_state;
  565.       get_reg = psr_register;
  566.       get_reg_arg = 0;
  567.       break;
  568.     default:
  569.       if (optim != NULL)
  570.         *optim = 1;
  571.       return;
  572.     }
  573.     }
  574.  
  575.   if ((*get_reg_state) (fi->frame_context, get_reg_arg))
  576.     {
  577.       if (raw_buffer != NULL)
  578.     *(int *)raw_buffer = (*get_reg) (fi->frame_context, get_reg_arg);
  579.       if (optim != NULL)
  580.     *optim = 0;
  581.       return;
  582.     }
  583.   else
  584.     {
  585.       if (optim != NULL)
  586.     *optim = 1;
  587.       return;
  588.     }
  589.  
  590.   /* Well, the caller can't treat it as a register or memory...  */
  591.   if (lvalp != NULL)
  592.     *lvalp = not_lval;
  593. }
  594.